package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoRedisService userInfoRedisService;
    @Autowired
    private ITravelCommentService travelCommentService;

    @RequestMapping("/query")
    public Object saveOrUpdate(TravelQuery qo){
        Page<Travel> page = travelService.query(qo);

        return JsonResult.success(page);
    }

    @RequiredLogin
    @RequestMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel,HttpServletRequest request){
        //2:当前登录用户信息
        String token = request.getHeader("token");
        UserInfo user = userInfoRedisService.getUserByToken(token);
        if(user != null){
            travel.setUserId(user.getId());
        }
        //最新游记id
        String id = travelService.saveOrUpdate(travel);
        return JsonResult.success(id);
    }

    /**
     * 获取当前登录用户信息
     * @param userInfo
     * @return
     */
    @GetMapping("/info")
    public Object info(@UserParam UserInfo userInfo){
        return JsonResult.success(userInfo);
    }

    /**
     * 对参数贴注解进行区分自定义参数解析器还是使用springmvc默认参数解析器
     * @param userInfo
     * @return
     */
    @GetMapping("/getUser")
    public Object getUser(@UserParam UserInfo userInfo){
        return JsonResult.success(userInfo);
    }

    @GetMapping("/updateUser")
    public Object updateUser(UserInfo userInfo){
        return JsonResult.success(userInfo);
    }


    @RequestMapping("/detail")
    public Object detail(String id){

        return JsonResult.success(travelService.get(id));
    }

    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }



    @RequestMapping("input")
    public Object input(Model model,String id){
        HashMap<String, Object> map = new HashMap<>();
        if (StringUtils.hasLength(id)){
            Travel travel = travelService.get(id);
            map.put("travel",travel);
        }

        List<Destination> destinations = destinationService.listAll();
        map.put("dests",destinations);

        return JsonResult.success(map);
    }

    //游记评论
    @RequiredLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment,  @UserParam  UserInfo userInfo){

        //设置用户信息
        BeanUtils.copyProperties(userInfo,travelComment);
        travelComment.setUserId(userInfo.getId());
        travelCommentService.save(travelComment);
        return JsonResult.success();
    }
    @GetMapping("/comments")
    public Object comments(String travelId){

        return JsonResult.success(travelCommentService.queryByTravelId(travelId));
    }
















}
